<?php

/**
 * getOfferedShifts Function
 *
 * Return all shifts being offered. Date & Time
 */
include_once 'UTerrorcode.php';
include_once 'UTcheckAuth.php';
include_once 'UTDBGlobal.php';

/**
 * @param $userName string
 * @param $userPass string
 * @return array
 */
function getOfferedShifts($userName, $userPassHash) {
    global $dbaddress;
    global $dbuser;
    global $dbpassword;
    global $dbdatabasename;

    $r = array();
    if (checkAuthentication($userName, $userPassHash) == true) {

        $conn = mysqli_connect($dbaddress, $dbuser, $dbpassword, $dbdatabasename);
        if (mysqli_connect_errno()) {
            $error = ErrorCode::sysError;
        } else {

            $sql = "SELECT * FROM roledate rd,roles r, employees e WHERE rd.RoleID=r.RoleID AND rd.empID=e.empID";
            $result = mysqli_query($conn, $sql);
            $row = mysqli_fetch_row($result);

            if (!$result) {
                $error = ErrorCode::sysError;
            } else {

                while ($row = mysqli_fetch_array($result)) {
                    $r [count($r)] = array('empFname' => $row['empFname'], 'empLname' => $row['empLname'], 'roleID' => $row['roleID'], 'shiftStatus' => $row['shiftStatus' != 'Occupied'], 'dateID' => $row['dateID'], 'startTime' => $row['startTime'], 'endTime' => $row['endTime']);
                }

                $error = ErrorCode::successRead;
                $output = array('code' => $error, 'value' => $r);
            }

            mysqli_close($conn);
        }
    } else {
        $error = ErrorCode::authFailRead;
        $output = array('code' => $error);
    }

    return $output;
}

?>